package com.miracle.设计模式.结构型模式.代理模式.JDK动态代理;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class HttpInvokeProxy2 implements InvocationHandler {

    Object object;

    /**
     * 将构造代理对象的代码放到这里
     *
     * @param object
     * @return
     */
    public Object getProxy(Object object) {
        this.object = object;
        Class<?> aClass = object.getClass();
        Object o = Proxy.newProxyInstance(aClass.getClassLoader(), aClass.getInterfaces(), this);
        return o;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result = method.invoke(object, args);
        after();
        return result;
    }

    /**
     * 前置处理
     */
    private void before() {
        System.out.println("前置加工");
    }

    /**
     * 后置处理
     */
    private void after() {
        System.out.println("后置加工");
    }
}
